import { FlowType } from '@babel/types'
import { breakDownIntersectionType, breakDownUnionType, IntersectionType, UnionTypeNodeType } from './decompositionType'
import { Type } from './giveTypeAnswer'

export function resolveType(typeInfo: FlowType & { typeAnnotation: string }): Type {
  const type = typeInfo.typeAnnotation
  // @ts-ignore
  if (!type) {
    return 'any'
  }
  // @ts-ignore
  switch (type.type) {
    // 联合类型
    case 'TSUnionType':
      return breakDownUnionType(type as UnionTypeNodeType)
    // TODO: 交叉类型
    // case 'TSIntersectionType':
    //   breakDownIntersectionType(type as IntersectionType)
    //   return ''
    case 'TSStringKeyword':
      return 'string'
    case 'TSNumberKeyword':
      return 'number'
    case 'TSBooleanKeyword':
      return 'boolean'
    default:
      return 'any'
  }
}

export function resolveReturnType(typeInfo: FlowType): Type {
  if (!typeInfo) return 'any'
  // @ts-ignore
  switch (typeInfo.type) {
    // 联合类型
    // @ts-ignore
    case 'TSUnionType':
      return breakDownUnionType(typeInfo as UnionTypeNodeType)
    // TODO: 交叉类型
    // case 'TSIntersectionType':
    //   breakDownIntersectionType(type as IntersectionType)
    //   return ''
    // @ts-ignore
    case 'TSStringKeyword':
      return 'string'
    // @ts-ignore
    case 'TSNumberKeyword':
      return 'number'
    // @ts-ignore
    case 'TSBooleanKeyword':
      return 'boolean'
    default:
      return 'any'
  }
}
